package com.windgo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.*;
import com.windgo.bean.bo.wxgoods.*;
import com.windgo.bean.bo.GoodsCreateBo;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.vo.*;
import com.windgo.mapper.*;
import com.windgo.util.StringUtil;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:ZhouXin
 * @Date:2022/10/11 20:04
 * @Description:
 **/
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;

    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;


    @Autowired
    MarketGrouponRulesMapper marketGrouponRulesMapper;

    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketFootprintMapper marketFootprintMapper;


    @Override
    public GoodsWxRelatedVo goodsWxRelated(Integer id) {
        Integer page=1;
        Integer limit=6;
        String sort="retail_price";
        String order="asc";
        //查询该商品类目id
        PageHelper.startPage(page,limit);
        Integer categoryId = marketGoodsMapper.selectCategoryIdById(id);
        //接下来查询相关商品
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        marketGoodsExample.setOrderByClause(sort+" "+order+" "+"limit 0,6");
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        ArrayList<WxCategoryGoodsVo> list = new ArrayList<>();
        for (MarketGoods marketGood : marketGoods) {
            if(marketGood.getId()!=id){
                WxCategoryGoodsVo wxCategoryGoodsVo = new WxCategoryGoodsVo();
                wxCategoryGoodsVo.setId(marketGood.getId());
                wxCategoryGoodsVo.setName(marketGood.getName());
                wxCategoryGoodsVo.setBrief(marketGood.getName());
                wxCategoryGoodsVo.setPicUrl(marketGood.getPicUrl());
                wxCategoryGoodsVo.setIsNew(marketGood.getIsNew());
                wxCategoryGoodsVo.setRetailPrice(marketGood.getRetailPrice());
                wxCategoryGoodsVo.setCounterPrice(marketGood.getCounterPrice());
                list.add(wxCategoryGoodsVo);
            }
        }
        PageInfo<WxCategoryGoodsVo> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return GoodsWxRelatedVo.data(total,pages,limit,page,list);
    }

    @Override
    public GoodsWxListVo goodsWxList(GoodsWxListBo goodsWxListBo, Integer id) {
        Integer categoryId = goodsWxListBo.getCategoryId();
        Integer brandId = goodsWxListBo.getBrandId();
        Boolean isHot = goodsWxListBo.getIsHot();
        Boolean isNew = goodsWxListBo.getIsNew();
        String keyword = goodsWxListBo.getKeyword();
        String sort = goodsWxListBo.getSort();
        String order = goodsWxListBo.getOrder();
        Integer page = goodsWxListBo.getPage();
        Integer limit = goodsWxListBo.getLimit();
        PageHelper.startPage(page,limit);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        if(categoryId!=null&&categoryId!=0){

            criteria.andCategoryIdEqualTo(categoryId);
        }
        if(brandId!=null){

            criteria.andBrandIdEqualTo(brandId);
        }
        if(isHot!=null){
            criteria.andIsHotEqualTo(isHot);
        }
        if(isNew!=null){
            criteria.andIsNewEqualTo(isNew);
        }
        if(keyword!=null&&!"".equals(keyword)){
            criteria.andNameLike("%"+keyword+"%");
        }
        if(sort!=null&&!"".equals(sort)&&order!=null&&!"".equals(order)){
            marketGoodsExample.setOrderByClause(sort+" "+order);
        }
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        ArrayList<WxCategoryGoodsVo> list = new ArrayList<>();
        for (MarketGoods marketGood : marketGoods) {
            WxCategoryGoodsVo wxCategoryGoodsVo = new WxCategoryGoodsVo();
            wxCategoryGoodsVo.setId(marketGood.getId());
            wxCategoryGoodsVo.setBrief(marketGood.getBrief());
            wxCategoryGoodsVo.setCounterPrice(marketGood.getCounterPrice());
            wxCategoryGoodsVo.setIsHot(marketGood.getIsHot());
            wxCategoryGoodsVo.setIsNew(marketGood.getIsNew());
            wxCategoryGoodsVo.setName(marketGood.getName());
            wxCategoryGoodsVo.setPicUrl(marketGood.getPicUrl());
            wxCategoryGoodsVo.setRetailPrice(marketGood.getRetailPrice());
            list.add(wxCategoryGoodsVo);
        }
        PageInfo<WxCategoryGoodsVo> wxCategoryGoodsVoPageInfo = new PageInfo<>(list);
        long total = wxCategoryGoodsVoPageInfo.getTotal();
        int pages = wxCategoryGoodsVoPageInfo.getPages();
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = marketCategoryExample.createCriteria();
        criteria1.andLevelEqualTo("L2");
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        if(!StringUtil.isEmpty(keyword)){
            MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
            MarketSearchHistoryExample.Criteria criteria2 = marketSearchHistoryExample.createCriteria();
            criteria2.andKeywordEqualTo(keyword);
            criteria2.andDeletedEqualTo(false);
            List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
            if(marketSearchHistories==null||marketSearchHistories.size()==0){
                MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
                Date date = new Date();
                marketSearchHistory.setDeleted(false);
                marketSearchHistory.setKeyword(keyword);
                marketSearchHistory.setAddTime(date);
                marketSearchHistory.setFrom("search");

                Integer id1 =id;
                //Integer userId=id1;
                marketSearchHistory.setUserId(id1);
                marketSearchHistory.setUpdateTime(date);
                marketSearchHistoryMapper.insertSelective(marketSearchHistory);
            }

        }
        return GoodsWxListVo.data(total,pages,limit,page,list,marketCategories);
    }

    @Override
    public Integer countNum() {
        Integer data = marketGoodsMapper.countNum();
        return data;
    }


    /**
     * 前台根据类目显示商品
     * @param id
     * @return
     */
    @Override
    public WxCategoryBo category(Integer id) {
        WxCategoryBo wxCategoryBo = new WxCategoryBo();
        //首先通过传入的id查询该类目
        MarketCategory currentCategory = marketCategoryMapper.selectByPrimaryKey(id);
        //在这里进行判断
        if(currentCategory.getPid()==0){
            Integer id1 = currentCategory.getId();
            MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
            criteria.andPidEqualTo(id1);
            List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
            wxCategoryBo.setBrotherCategory(marketCategories);
            int i=0;
            wxCategoryBo.setCurrentCategory(marketCategories.get(i));
            MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id1);
            wxCategoryBo.setParentCategory(marketCategory);
            return wxCategoryBo;
        }
        wxCategoryBo.setCurrentCategory(currentCategory);
        //由传入id查询父类类目id
        Integer pid = currentCategory.getPid();
        MarketCategory parentCategory = marketCategoryMapper.selectByPrimaryKey(pid);
        wxCategoryBo.setParentCategory(parentCategory);
        //由父类类目查询兄弟类目
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(pid);
        List<MarketCategory> brotherCategory = marketCategoryMapper.selectByExample(marketCategoryExample);
        wxCategoryBo.setBrotherCategory(brotherCategory);
        return wxCategoryBo;
        /*MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andIdEqualTo(id);
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);
        wxCategoryBo.setCurrentCategory(marketCategory);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        wxCategoryBo.setBrotherCategory(marketCategories);
        Integer pid = marketCategory.getPid();
        MarketCategory marketCategory1 = marketCategoryMapper.selectById(pid);
        wxCategoryBo.setParentCategory(marketCategory1);
        return wxCategoryBo;

         */

    }

    /**
     * 前台显示类目下的所有商品
     * @param categoryId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public WxGoodsListBo list(Integer categoryId, Integer page, Integer limit) {
       PageHelper.startPage(page,limit);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        ArrayList<WxMarketGoodsBo> wxMarketGoodsBos = new ArrayList<>();
        for (MarketGoods marketGood : marketGoods) {
            WxMarketGoodsBo wxMarketGoodsBo = new WxMarketGoodsBo(marketGood.getBrief(), marketGood.getCounterPrice(), marketGood.getId(), marketGood.getIsHot(), marketGood.getIsNew(), marketGood.getName(), marketGood.getPicUrl(), marketGood.getRetailPrice());
            wxMarketGoodsBos.add(wxMarketGoodsBo);
        }
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = marketCategoryExample.createCriteria();
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setLevel("L2");
        criteria1.andLevelEqualTo("L2");
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        WxGoodsListBo wxGoodsListBo = new WxGoodsListBo();
        wxGoodsListBo.setFilterCategoryList(marketCategories);
        wxGoodsListBo.setList(wxMarketGoodsBos);
        //PageInfo<T> tPageInfo = new PageInfo<>(wxGoodsListBo);
        PageInfo<MarketGoods> marketGoodsPageInfo = new PageInfo<>(marketGoods);
        int pageSize1 = marketGoodsPageInfo.getPageSize();
        long total1 = marketGoodsPageInfo.getTotal();
        int pageNum1 = marketGoodsPageInfo.getPageNum();
        long total2 = marketGoodsPageInfo.getPages();
        wxGoodsListBo.setPages((int)total2);
        wxGoodsListBo.setTotal((int)total1);
        wxGoodsListBo.setLimit(pageSize1);
        wxGoodsListBo.setPage(pageNum1);
        return wxGoodsListBo;
       /* PageInfo<MarketCategory> marketCategoryPageInfo = new PageInfo<>(marketCategories);
        int pages = marketCategoryPageInfo.getPages();
        int pageNum = marketCategoryPageInfo.getPageNum();
        long total = marketCategoryPageInfo.getTotal();
        int pageSize = marketCategoryPageInfo.getPageSize();

        */

    }


    /**
     * 商品模块的list
     *
     * @param info
     * @param goodsId
     * @param goodsSn
     * @param name
     * @return
     */
    @Override
    public CommonData<MarketGoods> list(BasePageInfo info, Integer goodsId, Integer goodsSn, String name) {



        //PageHelper.startPage(1, Integer.MAX_VALUE);
        PageHelper.startPage(info.getPage(), info.getLimit());

        //只根据goodsId,goodsSn,name做条件查询
        MarketGoodsExample example = new MarketGoodsExample();
        //排序
//        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        if (goodsId != null) {
            criteria.andIdEqualTo(goodsId);
        }
        if (goodsSn != null) {
            criteria.andGoodsSnLike("%" + goodsSn.toString() + "%");
        }

        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<MarketGoods> list = marketGoodsMapper.selectByExample(example);

        PageInfo<MarketGoods> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * 商品模块，根据id回显该商品有关的信息
     * @param id
     * @return
     */
    @Override
    public GoodsDetailVo detail(Integer id) {
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        //根据商品id从数据库中获取categoryID
        MarketGoods goods = marketGoodsMapper.selectByPrimaryKey(id);
        Integer categoryId = goods.getCategoryId();
        //利用categoryId去market_category中找id,pid
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(categoryId);
        Integer categoryId1 = marketCategory.getId();
        Integer pid = marketCategory.getPid();
        ArrayList<Integer> categoryIdList = new ArrayList<>();
        categoryIdList.add(categoryId1);
        categoryIdList.add(pid);
        //将categoryIdList封装到GoodsDetailVo对象中

        goodsDetailVo.setCategoryIds(categoryIdList);

        //将goods封装到GoodsDetailVo对象中
       goodsDetailVo.setGoods(goods);

        //根据商品id获取attributes的信息
        MarketGoodsAttributeExample example = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> attributes = marketGoodsAttributeMapper.selectByExample(example);
        //将attributes封装到GoodsDetailVo对象中
        goodsDetailVo.setAttributes(attributes);

        //根据id查询规格
        MarketGoodsSpecificationExample goodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria specificationExampleCriteria = goodsSpecificationExample.createCriteria();
        specificationExampleCriteria.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> specifications = marketGoodsSpecificationMapper.selectByExample(goodsSpecificationExample);
        //将specifications封装到GoodsDetailVo对象中
        goodsDetailVo.setSpecifications(specifications);

        //根据id获取商品库存信息
        MarketGoodsProductExample goodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria productExampleCriteria = goodsProductExample.createCriteria();
        productExampleCriteria.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> goodsProducts = marketGoodsProductMapper.selectByExample(goodsProductExample);
        goodsDetailVo.setProducts(goodsProducts);
        return goodsDetailVo;
    }

    /**
     * 显示所有的分类:
     * market_category
     * market_brand
     * @return
     */
    @Override
    public CatAndBrandVo catAndBrand() {
        //获取所有的brand信息
        List<BrandListVo> brandListVos=marketGoodsMapper.getAllBrand();

        //获取所有的category信息
        List<CategoryListVo> categoryListVos=marketGoodsMapper.getAllCategory();
        //根据categoryListVos的value去查找children
        List<ChildrenVo> children=new ArrayList<>();
        for (CategoryListVo categoryListVo : categoryListVos) {
            Integer value = categoryListVo.getValue();
            //根据分类id查询子类的信息
            children=marketGoodsMapper.getChildren(value);
            categoryListVo.setChildren(children);
        }

        //封装到CatAndBrandVo中
        CatAndBrandVo catAndBrandVo = new CatAndBrandVo();
        catAndBrandVo.setBrandList(brandListVos);
        catAndBrandVo.setCategoryList(categoryListVos);
        return catAndBrandVo;
    }

    /**
     * 添加商品
     * @param goodsCreateBo
     */
    @Override
    @Transactional
    public void create(GoodsCreateBo goodsCreateBo) {
        MarketGoods goods = goodsCreateBo.getGoods();
        Date date = new Date();
        goods.setAddTime(date);
        goods.setUpdateTime(date);
        //添加goods信息
        int i = marketGoodsMapper.insertSelective(goods);
        Integer goodsId = goods.getId();

        //获取规格信息的集合，进行添加
        List<MarketGoodsSpecification> specifications = goodsCreateBo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            //遍历对规格表进行添加
            specification.setGoodsId(goodsId);
            specification.setAddTime(date);
            specification.setUpdateTime(date);
            marketGoodsSpecificationMapper.insertSelective(specification);
        }

        //获取商品数量的信息，进行添加
        List<MarketGoodsProduct> products = goodsCreateBo.getProducts();
        for (MarketGoodsProduct product : products) {
            product.setGoodsId(goodsId);
            product.setAddTime(date);
            product.setUpdateTime(date);
            //遍历，对商品数量进行添加
            marketGoodsProductMapper.insertSelective(product);
        }

        //获取商品attribute，进行添加
        List<MarketGoodsAttribute> attributes = goodsCreateBo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsId);
            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            //遍历，对attribute进行添加
            marketGoodsAttributeMapper.insertSelective(attribute);
        }


    }

    /**
     * 更新商品信息
     * @param goodsCreateBo
     */
    @Override
    @Transactional
    public void update(GoodsCreateBo goodsCreateBo) {
        //获取goods信息
        MarketGoods goods = goodsCreateBo.getGoods();
        //对goods信息进行更新
        marketGoodsMapper.updateByPrimaryKeySelective(goods);

        //获取specifications信息
        List<MarketGoodsSpecification> specifications = goodsCreateBo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            //遍历修改
            marketGoodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }

        //获取products信息
        List<MarketGoodsProduct> products = goodsCreateBo.getProducts();
        for (MarketGoodsProduct product : products) {
            //遍历修改
            marketGoodsProductMapper.updateByPrimaryKeySelective(product);
        }

        //获取attribute
        List<MarketGoodsAttribute> attributes = goodsCreateBo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            //遍历修改
            marketGoodsAttributeMapper.updateByPrimaryKeySelective(attribute);
        }

    }

    /**
     * 根据id进行删除
     * @param goodsId
     */
    @Override
    @Transactional
    public void delete(Integer goodsId) {
        //删除商品的信息
        marketGoodsMapper.deleteByPrimaryKey(goodsId);

        //删除商品对应的specifications信息
        //marketGoodsSpecificationMapper.deleteByGoodsId(goodsId);
        MarketGoodsSpecificationExample specificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria specificationExampleCriteria = specificationExample.createCriteria();
        specificationExampleCriteria.andGoodsIdEqualTo(goodsId);
        marketGoodsSpecificationMapper.deleteByExample(specificationExample);


        //删除商品对应的products信息
        //marketGoodsProductMapper.deleteByExample(goodsId);
        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria productExampleCriteria = productExample.createCriteria();
        productExampleCriteria.andGoodsIdEqualTo(goodsId);
        marketGoodsProductMapper.deleteByExample(productExample);

        //删除商品对应的attribute信息
        MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria attributeExampleCriteria = attributeExample.createCriteria();
        attributeExampleCriteria.andGoodsIdEqualTo(goodsId);
        marketGoodsAttributeMapper.deleteByExample(attributeExample);
    }

    /**
     * list接口中limit为0的情况
     * @return
     */
    @Override
    public CommonData<MarketGoods> listLimit() {
        PageHelper.startPage(1, Integer.MAX_VALUE);
        MarketGoodsExample example = new MarketGoodsExample();
        List<MarketGoods> list = marketGoodsMapper.selectByExample(example);

        PageInfo<MarketGoods> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * 小程序中根据商品id显示商品详情
     * @param id
     * @return
     */
    @Override
    public WxGoodsDetailsVo wxDetail(Integer id) {
        //创建响应体对象
        WxGoodsDetailsVo data = new WxGoodsDetailsVo();

        //根据id,先去goods表中获取信息
        MarketGoods goods = marketGoodsMapper.selectByPrimaryKey(id);
        //封装specificationList

        ArrayList<WxGoodsDetailsSpecificationList> specificationLists = new ArrayList<>();

        //创建这个集合对应的对象
        WxGoodsDetailsSpecificationList wxGoodsDetailsSpecificationList = new WxGoodsDetailsSpecificationList();

        //name:商品名；valueList:商品对应的规格信息

        //赋值name
        String name = goods.getName();
        wxGoodsDetailsSpecificationList.setName(name);

        //根据id去规格表中查规格信息，赋值给valueList
        MarketGoodsSpecificationExample specificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria = specificationExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> specifications = marketGoodsSpecificationMapper.selectByExample(specificationExample);
        wxGoodsDetailsSpecificationList.setValueList(specifications);

        specificationLists.add(wxGoodsDetailsSpecificationList);

        //给响应体的specificationList赋值
        data.setSpecificationList(specificationLists);

        //TODO  处理groupon
        //这里逻辑是从团购规则表中查是否有该商品的团购信息，如果没有则赋值false

        //根据id去团购规则表中查  marketGrouponRules
        MarketGrouponRulesExample rulesExample = new MarketGrouponRulesExample();
        MarketGrouponRulesExample.Criteria criteria1 = rulesExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        List<MarketGrouponRules> marketGrouponRules = marketGrouponRulesMapper.selectByExample(rulesExample);
        ArrayList<MarketGrouponRules> marketGrouponRules1 = new ArrayList<>();
        if (marketGrouponRules!=null){
            for (MarketGrouponRules rule : marketGrouponRules) {
                marketGrouponRules1.add(rule);
            }
        }
        data.setGroupon(marketGrouponRules1);

        //TODO  issue：常见问题
        //去issue中查就行了
        MarketIssueExample issueExample = new MarketIssueExample();
        List<MarketIssue> issues = marketIssueMapper.selectByExample(issueExample);
        data.setIssue(issues);

        //TODO   userHasCollect
        //从session中获取userId,通过userId和id去收藏表collect中查询是否有收藏
        //获取userId
        //登录情况
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if(principals!=null){
            MarketUser primaryPrincipal1 = (MarketUser) principals.getPrimaryPrincipal();
            Integer userId = primaryPrincipal1.getId();
            //TODO  添加足迹
            //添加到footprint表中
            //id就是goodsId
            //获取userId
            MarketFootprint footprint = new MarketFootprint();
            footprint.setUserId(userId);
            footprint.setGoodsId(id);
            Date date = new Date();
            footprint.setAddTime(date);
            footprint.setUpdateTime(date);
            marketFootprintMapper.insertSelective(footprint);

            MarketCollectExample collectExample = new MarketCollectExample();
            MarketCollectExample.Criteria criteria2 = collectExample.createCriteria();
            criteria2.andUserIdEqualTo(userId);
            criteria2.andValueIdEqualTo(id);
            List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(collectExample);
            if (marketCollects!=null){
                data.setUserHasCollect(0);
            }else {
                data.setUserHasCollect(1);
            }

            //TODO  comment
            //comment中对应的对象是WxGoodsDetailCommentVo   data,count
            WxGoodsDetailCommentVo commentVo = new WxGoodsDetailCommentVo();
            //先查询有评论的数量
            MarketCommentExample commentExample = new MarketCommentExample();
            MarketCommentExample.Criteria criteria3 = commentExample.createCriteria();
            criteria3.andValueIdEqualTo(id);
            List<MarketComment> marketCommentList = marketCommentMapper.selectByExample(commentExample);
            int count = marketCommentList.size();
            commentVo.setCount(count);
            //再获取data
            //创建Comment对应的data对应的集合
            ArrayList<WxGoodDetailsCommentDataVo> commentDataVos = new ArrayList<>();
            //根据用户id去user表中查nickname,avatar
            MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
            String nickname = marketUser.getNickname();
            String avatar = marketUser.getAvatar();

            int i=0;
            //从评论表中获取信息
            for (MarketComment marketComment : marketCommentList) {
                if (i<2){
                    WxGoodDetailsCommentDataVo commentDataVo = new WxGoodDetailsCommentDataVo();
                    commentDataVo.setNickname(nickname);
                    commentDataVo.setAvatar(avatar);
                    commentDataVo.setAddTime(marketComment.getAddTime());
                    commentDataVo.setAdminContent(marketComment.getAdminContent());
                    commentDataVo.setContent(marketComment.getContent());
                    commentDataVo.setId(marketComment.getId());
                    commentDataVo.setPicList(marketComment.getPicUrls());
                    commentDataVos.add(commentDataVo);
                    i++;
                }
            }
            commentVo.setData(commentDataVos);
            data.setComment(commentVo);

            //TODO  处理share   是否有分享海报
            String shareUrl = goods.getShareUrl();
            if (StringUtil.isEmpty(shareUrl)){
                data.setShare(false);
            }else {
                data.setShare(true);
            }


            //TODO   attribute  goodsId建立联系的
            MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
            MarketGoodsAttributeExample.Criteria criteria4 = attributeExample.createCriteria();
            criteria4.andGoodsIdEqualTo(id);
            List<MarketGoodsAttribute> attributes = marketGoodsAttributeMapper.selectByExample(attributeExample);
            data.setAttribute(attributes);

            //TODO brand
            Integer brandId = goods.getBrandId();
            MarketBrand marketBrand = marketBrandMapper.selectByPrimaryKey(brandId);
            data.setBrand(marketBrand);

            //TODO productList
            MarketGoodsProductExample productExample = new MarketGoodsProductExample();
            MarketGoodsProductExample.Criteria criteria5 = productExample.createCriteria();
            criteria5.andGoodsIdEqualTo(id);
            List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(productExample);
            data.setProductList(marketGoodsProducts);
            data.setInfo(goods);
            return data;
        }
        //MarketUser primaryPrincipal1 = (MarketUser) principals.getPrimaryPrincipal();
       // Integer userId = primaryPrincipal1.getId();
        MarketCollectExample collectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria2 = collectExample.createCriteria();


       // criteria2.andUserIdEqualTo(userId);
        criteria2.andValueIdEqualTo(id);

        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(collectExample);
        if (marketCollects!=null){
            data.setUserHasCollect(0);
        }else {
            data.setUserHasCollect(1);
        }

        //TODO  comment
        //comment中对应的对象是WxGoodsDetailCommentVo   data,count
        WxGoodsDetailCommentVo commentVo = new WxGoodsDetailCommentVo();
        //先查询有评论的数量
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria3 = commentExample.createCriteria();
        criteria3.andValueIdEqualTo(id);
        List<MarketComment> marketCommentList = marketCommentMapper.selectByExample(commentExample);
        int count = marketCommentList.size();
        commentVo.setCount(count);
        //再获取data
        //创建Comment对应的data对应的集合
        ArrayList<WxGoodDetailsCommentDataVo> commentDataVos = new ArrayList<>();
        //根据用户id去user表中查nickname,avatar
        //MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
        String nickname = null;
        String avatar = null;

        int i=0;
        //从评论表中获取信息
        for (MarketComment marketComment : marketCommentList) {
            if (i<2){
                WxGoodDetailsCommentDataVo commentDataVo = new WxGoodDetailsCommentDataVo();
                commentDataVo.setNickname(nickname);
                commentDataVo.setAvatar(avatar);
                commentDataVo.setAddTime(marketComment.getAddTime());
                commentDataVo.setAdminContent(marketComment.getAdminContent());
                commentDataVo.setContent(marketComment.getContent());
                commentDataVo.setId(marketComment.getId());
                commentDataVo.setPicList(marketComment.getPicUrls());
                commentDataVos.add(commentDataVo);
                i++;
            }
        }
        commentVo.setData(commentDataVos);
        data.setComment(commentVo);

        //TODO  处理share   是否有分享海报
        String shareUrl = goods.getShareUrl();
        if (StringUtil.isEmpty(shareUrl)){
            data.setShare(false);
        }else {
            data.setShare(true);
        }


        //TODO   attribute  goodsId建立联系的
        MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria4 = attributeExample.createCriteria();
        criteria4.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> attributes = marketGoodsAttributeMapper.selectByExample(attributeExample);
        data.setAttribute(attributes);

        //TODO brand
        Integer brandId = goods.getBrandId();
        MarketBrand marketBrand = marketBrandMapper.selectByPrimaryKey(brandId);
        data.setBrand(marketBrand);

        //TODO productList
        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria5 = productExample.createCriteria();
        criteria5.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(productExample);
        data.setProductList(marketGoodsProducts);

        //TODO  info
        data.setInfo(goods);






        return data;
    }


}
